package com.itfallrain.controller;

import com.itfallrain.common.MD5Encode;
import com.itfallrain.entity.User;
import com.itfallrain.service.LoginService;
import com.itfallrain.service.MainService;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private MainService mainService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
//    权限验证
    public Object login(String login_id, String password,HttpSession session) {
//        调用登录方法进行调用
        User user = loginService.login(login_id,password);

//        System.out.println("id:" + login_id);
//        System.out.println("pwd:" + password);
        Map<String, Object> map = new HashMap();
        if (user != null) {
            map.put("code", 1);
            map.put("data", user);
            session.setAttribute("login_id",user);
            return map;
        }
        map.put("code", 0);
        map.put("msg", "用户名或密码错误");
        return map;
    }
//    修改密码
    @RequestMapping("/repwd")
    @ResponseBody
    public Object modify(String new_password,HttpSession session){
        User users = (User) session.getAttribute("login_id");
        Integer id = users.getId();
        String loginId = users.getLoginId();
        User u=new User();
        u.setLoginId(loginId);
        u.setId(id);
        Map<String,Object> map=new HashMap<String, Object>();
//        把数据存进User对象中
        u.setPassword(MD5Encode.encode(new_password));
        int i=mainService.repwd(u);
        if(i==1){
            map.put("code",1);
            map.put("msg","修改成功！");

        }else {
            map.put("code",0);
            map.put("msg","修改失败");
        }
        return map;
    }


}
